﻿/*
   Copyright 2012 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Flower.Workflow;

namespace Flower.Processing.Activities
{
    [SaveAfter]
    [Transactional]
    [Retry]
    public class Invoke<TSvc> : Activity, IExecutionFlagsHolder
    {
        internal Func<IContext, string> ServicePath { get; set; }
        internal Action<IContext, TSvc> Call { get; set; }
        internal TSvc Service { get; set; }

        private readonly ExecutionFlags _executionFlags;

        public ExecutionFlags ExecutionFlags
        {
            get { return _executionFlags; }
        }

        internal Invoke(ExecutionFlags executionFlags)
        {
            _executionFlags = executionFlags;
        }

        public override bool IsBlocking
        {
            get { return true; }
        }

        public override Interruption Do(Processor processor)
        {
            Call
            (
                processor, 
                (ServicePath != null) ?
                    processor.ServicesProvider.GetService<TSvc>(ServicePath(processor)) :
                    Service
            );

            ++processor.State.__ActivityPointer;
            return null;
        }
    }
}
